/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gruppe7.servlets;

import gruppe7.data.Ticket;
import gruppe7.data.UserController;
import gruppe7.util.Html;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author bertram
 */
public class PurchaseTicket extends HttpServlet {

    private Html htmlInclude = new Html();


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");
        UserController uc = UserController.getInstance();

        out.println(htmlInclude.setHeader("Purchase Ticket"));
        rd.include(request, response);

        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");

        if (uc.isValidSession(request)) {
            String departure = (String) request.getParameter("departure");
            String destination = (String) request.getParameter("destination");
            float price = Float.parseFloat(request.getParameter("price"));
            Ticket t = new Ticket(uc.getSessionUser(request).getUsername(),
                    price, departure, destination);
            if (t.isValid()) {
                out.println("you bought a ticket from "+departure+" to "+
                        destination+" for "+price+" fantoms.<br>");
            } else {
                out.println("you have no money");
            }
        } else {
            out.println("crap");
        }
        out.println("</div></div>");
        out.println(htmlInclude.setFooter());


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
